package com.shenfa.service;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;

import com.shenfa.bean.City;
import com.shenfa.bean.CostPar;
import com.shenfa.bean.Epiboly;
import com.shenfa.bean.PaperCityCrd;
import com.shenfa.bean.TreeNode;
import com.shenfa.bean.TreeNodeCheck;
import com.shenfa.dao.CityDao;
import com.shenfa.dao.CostParDao;
import com.shenfa.dao.EpibolyDao;

/**
 * 城市信息管理业务逻辑类
 * 
 * @author yanglong
 * 
 * @date 2011-08-19
 *
 */
public class CityService {

	@Autowired
	private CityDao cityDao;
	@Autowired
	private EpibolyDao epibolDao;
	@Autowired
	private CostParDao costParDao;
	
	/**
	 * 查询所有城市
	 * @return
	 */
	public List<City> queryAllCity() {
		List<City> list = new ArrayList<City>();
		list = cityDao.queryAllList();
		return list;
	}

	/**
	 * 根据城市编号查询所有城市
	 * @param cityno
	 * @return
	 */
	public List<City> queryListCityByParentno(City city) {
		List<City> list = new ArrayList<City>();
		list = cityDao.queryListByParentno(city);
		return list;
	}

	/**
	 * 添加城市
	 * @param city
	 */
	public int insertCity(City city) {
		int num=cityDao.insertCity(city);
		if(num==0){
			List<Epiboly> epiboly=epibolDao.getAllList();
			CostPar costPar=new CostPar();
//			costPar.setCityname(city.getCityname());
			costPar.setCityno(city.getCityno());
			costPar.setEnvcost(0);
			costPar.setEnvelop(0);
			costPar.setEnvlopweight(0);
			costPar.setPrint(0);
			costPar.setReckoning(0);
			costPar.setTotalweight(0);
			if(epiboly.size()>0){
				for (Epiboly epiboly2 : epiboly) {
	//				costPar.setEpibolyname(epiboly2.getName());
					costPar.setEpibolyno(epiboly2.getId());
					int ret = costParDao.add(costPar);
					if(ret!=0){
						num = -1;
					}
				}
			}else{
				num = -1;
			}
		}
		return num;
	}
	
	/**
	 * 更新城市信息
	 */
	public int updateCity(City city){
		return cityDao.updateCity(city);
	}
	
	/**
	 * 删除城市信息
	 * @param city
	 */
	public int deleteCity(City city){
		int num=cityDao.deleteCity(city);
		if(num==0){
			CostPar costPar=new CostPar();
			costPar.setCityno(city.getCityno());			
			int ret = costParDao.del(costPar);
			if(ret!=0){
				num = -1;
			}
		}
		return num;
	}

	public List<TreeNode> queryTree(String node) {
		List<TreeNode> list = cityDao.queryTree(node);
		return list;
	}
	
	public List<TreeNodeCheck> queryCheckTree(String node) {
		List<TreeNodeCheck> list = cityDao.queryCheckTree(node);
		return list;
	}

	/**
	 * 根据对象条件查询对象集合
	 * @param city	查询对象
	 * @return
	 */
	public List<City> queryList(City city, int start, int limit) {
		List<City> list = new ArrayList<City>();
		list = cityDao.queryList(city, start, limit);
		return list;
	}

	/**
	 * 查询总记录条数
	 * @param city	查询条件
	 * @return	总记录条数
	 */
	public int queryCount(City city) {
		return cityDao.queryCount(city);
	}

}
